//
//  XMCameraTimeTableViewCell.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/4/22.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMCameraTimeTableViewCell.h"
#import "KBaseCollectionView.h"
#import "XMCameraTimeCollectionViewCell.h"


@interface XMCameraTimeTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) KBaseCollectionView * collectionView;

@property (nonatomic,weak) KV2GetCameraAngleCateModel * selectModel;


@end

@implementation XMCameraTimeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}

-(void)setUI{
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.equalTo(self);
    }];
}


#pragma mark ----collection----

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMCameraTimeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[XMCameraTimeCollectionViewCell className] forIndexPath:indexPath];
    
    KV2GetCameraAngleCateModel * model = self.listArr[indexPath.row];
    
    cell.itemSelected = model.isSelected;
    cell.titleLabel.text = model.name;
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.listArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat  LRMargin = 20;

    NSInteger itemW = (kScreenWidth-LRMargin*4)/3;
    
    return CGSizeMake(itemW, 38);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(10,20,10,20);//分别为上、左、下、右
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    KV2GetCameraAngleCateModel * model = self.listArr[indexPath.row];

    
    if (self.selectModel != model) {

        model.isSelected = YES;
        self.selectModel.isSelected = NO;
        
        if (self.clickItemBlock) {
            self.clickItemBlock(model);
        }
        
        self.selectModel = model;
        [self.collectionView reloadData];
        
    }
}


#pragma mark ----getting
//列表
-(KBaseCollectionView *)collectionView{
    
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumLineSpacing = 20;
        layout.minimumInteritemSpacing = 10;
        _collectionView = [[KBaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = kWhiteColor;

        [self addSubview:_collectionView];
        
        [_collectionView registerClass:[XMCameraTimeCollectionViewCell class] forCellWithReuseIdentifier:[XMCameraTimeCollectionViewCell className]];
        
    }
    return _collectionView;
}



-(void)setType:(NSInteger)type{
    _type = type;
    [self.collectionView reloadData];
}

-(void)setListArr:(NSArray *)listArr{
    _listArr = listArr;
    [self setUI];
    
    if (listArr.count>0) {
        for (KV2GetCameraAngleCateModel * model in listArr) {
            if (model.isSelected) {
                self.selectModel = model;

            }
        }
    }

}

-(void)dealloc{
    
    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
